package pers.csw.lookBook.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import pers.csw.lookBook.mapper.BooksMapper;
import pers.csw.lookBook.mapper.ChapterMapper;
import pers.csw.lookBook.mapper.ReadHistoryMapper;
import pers.csw.lookBook.dto.ReadHistoryAndBookDto;
import pers.csw.lookBook.entity.Books;
import pers.csw.lookBook.entity.Chapter;
import pers.csw.lookBook.entity.ReadHistory;
import pers.csw.lookBook.service.ReadHistoryService;

import java.util.ArrayList;
import java.util.List;

@Service
public class ReadHistoryServiceImpl extends ServiceImpl<ReadHistoryMapper, ReadHistory> implements ReadHistoryService {

    @Autowired
    ReadHistoryMapper readHistoryMapper;
    @Autowired
    BooksMapper booksMapper;
    @Autowired
    ChapterMapper chapterMapper;

    @Override
    public ReadHistory getReadHistoryByUserIdAndBookId(Integer userId, Integer bookId) {
        return readHistoryMapper.getReadHistoryByUserIdAndBookId(userId,bookId);
    }

    @Override
    public List<ReadHistoryAndBookDto> getReadHistoryList(Integer userId) {
        List<ReadHistoryAndBookDto> readHistoryAndBookDtos =new ArrayList<>();
        readHistoryMapper.getReadHistoryList(userId).forEach(readHistory -> {
            ReadHistoryAndBookDto readHistoryAndBookDto  = new ReadHistoryAndBookDto();
            Books books = booksMapper.selectById(readHistory.getBookId());
            Chapter chapter = chapterMapper.selectById(readHistory.getChapterId());
            readHistoryAndBookDto.setReadHistory(readHistory);
            readHistoryAndBookDto.setBooks(books);
            readHistoryAndBookDto.setChapter(chapter);
            readHistoryAndBookDtos.add(readHistoryAndBookDto);
        });
        return readHistoryAndBookDtos;
    }
}
